<?php
namespace User\Controller;
use Common\BLL\SMSBLL;
use Common\Controller\HomebaseController;
use Common\Lib\RedisHandle;

class LoginController extends HomebaseController {

    public function _initialize() {
        parent::_initialize();
        if (cookie('user_name') != null){
            $this->redirect('/');
        }
    }

    //前台登录
    public function index(){
        $par = I('get.par');
        $this->assign('par',$par);
        $this->display();
    }

    //登录提交
    public function dologin(){
        $user_name = I('post.userName');
        $sms_code = I('post.smsValidateCode');
        $img_code = I('post.imgValidateCode');
        if(!preg_match("/^1[34578]\d{9}$/",$user_name)){
            $this->ajaxReturn(array('success'=>false,'message'=>'请输入正确的手机号'));
        }
        $smsbll = new SMSBLL();
        $result = $smsbll->verifyCode($user_name,$sms_code);
        if (!$result['success']){
            $this->ajaxReturn(array('success'=>false,'message'=>'短信验证码错误'));
        }
        if (!sp_check_verify_code($img_code)){
            $this->ajaxReturn(array('success'=>false,'message'=>'图形验证码错误'));
        }
        $status = RedisHandle::getUser($user_name);
        if ($status == null){
             RedisHandle::setUser($user_name,I('post.'),200);
        }
        cookie('user_name',$user_name);
        $this->ajaxReturn(array('success'=>true));
    }



}


